package com.aauedu.auth.controller;


import com.aauedu.auth.service.SysRoleService;
import com.aauedu.auth.service.SysUserRoleService;
import com.aauedu.auth.service.SysUserService;
import com.aauedu.common.result.Result;
import com.aauedu.common.utils.MD5;
import com.aauedu.model.system.SysRole;
import com.aauedu.model.system.SysUser;
import com.aauedu.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 鲍宪立
 * @since 2024-03-11
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    //用户条件分页查询
    @ApiOperation("用户条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit, SysUserQueryVo sysUserQueryVo) {
        //创建Page对象
        Page<SysUser> pageParam = new Page<>(page, limit);
        //封装条件，判断条件值不为空
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(sysUserQueryVo.getKeyword())) {
            //like:模糊查询
            sysUserLambdaQueryWrapper.like(SysUser::getUsername, sysUserQueryVo.getKeyword());
        }
        if (!StringUtils.isEmpty(sysUserQueryVo.getCreateTimeBegin())) {
            //ge:大于等于
            sysUserLambdaQueryWrapper.ge(SysUser::getCreateTime, sysUserQueryVo.getCreateTimeBegin());
        }
        if (!StringUtils.isEmpty(sysUserQueryVo.getCreateTimeEnd())) {
            //le:小于等于
            sysUserLambdaQueryWrapper.le(SysUser::getUpdateTime, sysUserQueryVo.getCreateTimeEnd());
        }

        //调用mp的方法实现条件分页查询
        IPage<SysUser> pageModel = sysUserService.page(pageParam, sysUserLambdaQueryWrapper);
        return Result.ok(pageModel);
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("获取用户")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        SysUser sysUser = sysUserService.getById(id);
        return Result.ok(sysUser);
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation("新增用户")
    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser) {
        //对密码进行加密存储，使用MD5加密
        String password = sysUser.getPassword();
        String passwordMD5 = MD5.encrypt(password);
        sysUser.setPassword(passwordMD5);
        boolean flag = sysUserService.save(sysUser);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysUser sysUser) {
        boolean flag = sysUserService.updateById(sysUser);
        if (flag) {
           return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation("删除用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean flag = sysUserService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation("批量删除用户")
    @DeleteMapping("/batchDelete/{ids}")
    public Result batchDelete(@PathVariable List<Integer> ids) {
        boolean flag = sysUserService.removeByIds(ids);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("更新用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }



}

